package v1

import (
	"ginBlog/service"
    "ginBlog/utils"
    "github.com/gin-gonic/gin"
    "net/http"
    "strconv"
)

func GetAllTags(c *gin.Context)  {
    limit := c.DefaultQuery("limit", "5")
    page := c.DefaultQuery("page", "1")
    tags, e := service.Tag{}.GetAll(limit, page)
    if e != nil {
        c.JSON(http.StatusInternalServerError, utils.GenResponse(40008, nil, e))
    }
    c.JSON(http.StatusOK, utils.GenResponse(20000, tags, nil))
}

func CreateTag(c *gin.Context)  {
    tag := &service.Tag{}
    if e := c.ShouldBindJSON(tag); e != nil {
        c.JSON(http.StatusBadRequest, utils.GenResponse(40000, nil, e))
    }
    t, e := tag.Create()
    if e != nil {
        c.JSON(http.StatusInternalServerError, utils.GenResponse(40009, nil, e))
        return
    }
    c.JSON(http.StatusOK, utils.GenResponse(20000, t, nil))
}

func DeleteTag(c *gin.Context)  {
    id,_ := strconv.Atoi(c.Param("id"))
    r := service.Tag{ID: id}

    tag, i := r.GetOne()
    if i != nil {
        c.JSON(http.StatusNotFound, utils.GenResponse(40006, nil, i))
        return
    }

    if e := tag.Delete(); e != nil {
       c.JSON(http.StatusInternalServerError, utils.GenResponse(40011, nil, e))
        return
    }
    c.JSON(http.StatusOK, utils.GenResponse(20000, tag, nil))
}

func EditTag(c *gin.Context)  {
    id,_ := strconv.Atoi(c.Param("id"))
    t := service.Tag{ID: id}

    if e := c.ShouldBindJSON(&t); e != nil {
        c.JSON(http.StatusBadRequest, utils.GenResponse(40000, nil, e))
    }

    if e := t.Edit(); e != nil {
        c.JSON(http.StatusInternalServerError, utils.GenResponse(40010, nil, e))
        return
    }
    c.JSON(http.StatusOK, utils.GenResponse(20000, t, nil))
}